//import { queryNotices } from '@/services/user';
import routeList from '../../config/routes';
import { findRoute } from '@/utils';
const GlobalModel = {
  namespace: 'global',
  state: {
    routerHistoryList: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // 这里的方法名可以随便命名，当监听有变化的时候就会依次执行这的变化,这里的dispatch和history和之前说的是一样的
      history.listen(({ pathname }) => {
        const currentRoute = findRoute(routeList, pathname, pathname);
        if (!history.location.state?.noPush && currentRoute && currentRoute.title) {
          dispatch({
            type: 'addRouterHistory',
            payload: currentRoute,
          });
        }
      });
    },
  },

  reducers: {
    addRouterHistory(state, { payload }) {
      const { routerHistoryList } = state;
      const routeList = [...routerHistoryList];
      const delIndex = routeList.findIndex(item=>item.path==payload.path);
      if(delIndex!==-1){
        routeList.splice(delIndex,1);
      }
      if(routeList.length>=8) routeList.shift()
      routeList.push(payload);  
      return {
        ...state,
        routerHistoryList: routeList,
      };
    },
    delRouterHistory(state,{payload}){
      const { routerHistoryList } = state;
      const routeList = [...routerHistoryList];
      const delIndex = routeList.findIndex(item=>item.name==payload.name);
      if(delIndex!==-1) routeList.splice(delIndex,1);
      return {
        ...state,
        routerHistoryList: routeList,
      };
    }
  },
  effects: {
    // *changeDemo(action,{put}){
    //     const {payload} = action;
    //     yield put({
    //         type:"demo",
    //         payload:payload
    //     })
    // }
  },
};
export default GlobalModel;
